package com.example.hospitalmanagement.viewmodel

import android.util.Log
import androidx.lifecycle.ViewModel
import com.example.hospitalmanagement.BaseApplication
import com.example.hospitalmanagement.bean.ChargeData
import rx.Observable
import rx.android.schedulers.AndroidSchedulers
import rx.schedulers.Schedulers

class ChargeViewModel : ViewModel() {
    fun getChargeByName(
        map: Map<String, String>,
        listener: (dataList: MutableList<ChargeData>) -> Unit
    ) {
        val dataList: MutableList<ChargeData> = mutableListOf()
        BaseApplication.iChargeRequest.requestChargeByName(map)
            .flatMap {
                Observable.from(it.data)
            }?.map {
                ChargeData(
                    chargeId = it.chargeId,
                    itemName = it.itemName,
                    itemMomey = it.itemMomey,
                    itemStatus = it.itemStatus,
                    itemType = it.itemType,
                    sourceId = it.sourceId,
                    checked = false
                )
            }?.subscribeOn(Schedulers.io())
            ?.observeOn(AndroidSchedulers.mainThread())
            ?.subscribe({
                dataList.add(it)
            }, {
                Log.e("ChargeViewModel", it.message, it)
            }, {
                listener(dataList)
            })
    }
}